// src/app/page.tsx
'use client';
import UserButton from "@/features/auth/components/user-button";
import {useGetWorkspaces} from "@/features/workspaces/api/use-get-workspaces";
import {useEffect, useMemo, useState} from "react";
import {useCreateWorkspaceModal} from "@/features/workspaces/store/use-create-workspace-modal";
import {useRouter} from "next/navigation";
import {useCurrentUser} from "@/hooks/use-current-user";

export default function Home() {
    const router = useRouter()

    const [open, setOpen] = useCreateWorkspaceModal()

    // const [workspaces, setWorkspaces] = useState();

    // useMemo is a React Hook that lets you cache the result of a calculation between re-renders.
    // useMemo 是一个 React Hook，它允许你在两次重新渲染之间缓存计算结果。
    // @ts-ignore
    // const workspacesId = useMemo(() => workspaces?.id, [workspaces])

    const user = useCurrentUser()

    useEffect(() => {
        // if (!workspaces) {
        //     return
        // }
        (async () => {
            let w = await useGetWorkspaces({uid: user?.id})

            if (!w || w.length === 0) {
                setOpen(true)
                return
            }
            if (!w[0]?.id && !open) {
                setOpen(true)
                return
            }

            if (w[0].id) {
                router.replace(`/social/workspace/${w[0].id}`)
                return
            }
        })()

    }, [open, setOpen, router]);

    return (
        <div>
            <UserButton/>
        </div>
    );
}

